﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Model;
using System.Text.RegularExpressions;
using DBUtility;
public partial class BaseData_UserCommentManagement : System.Web.UI.Page
{
    List<ListItem> statusList = new List<ListItem>();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ListItem unshow = new ListItem("隐藏", "0");
        ListItem show = new ListItem("显示", "1");
        statusList.Add(unshow);
        statusList.Add(show);
        if (!IsPostBack)
        {

            GetAllComment();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        GetAllComment();
        ((TextBox)this.GridView1.Rows[e.NewEditIndex].Cells[6].FindControl("txtJF")).Enabled = true;
        ((DropDownList)this.GridView1.Rows[e.NewEditIndex].Cells[8].FindControl("DdrpStatus")).Enabled = true;

        Model.COMMODITY_EVAL eval;
        int ceid = SiteCommon.Common.GetInt(this.GridView1.DataKeys[e.NewEditIndex].Value);
        string ret = new BLL.UserCommonBL().getUserCommentById(ceid, out eval);
        if (ret == "000000")
        {
            if (eval.BYZD1 == 1)
            {
                ((TextBox)this.GridView1.Rows[e.NewEditIndex].Cells[6].FindControl("txtJF")).Enabled = false; 
            }
            
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        decimal jf = decimal.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].FindControl("txtJF")).Text);
        int status = ((DropDownList)this.GridView1.Rows[e.RowIndex].Cells[8].FindControl("DdrpStatus")).SelectedIndex;
        int ceid = SiteCommon.Common.GetInt(this.GridView1.DataKeys[e.RowIndex].Value);


        Model.COMMODITY_EVAL eval;
        if (ceid != -1)
        {
            string ret = new BLL.UserCommonBL().getUserCommentById(ceid, out eval);
            if (ret == "000000")
            {
                Model.V_VIPSET vipset = new BLL.V_VIPSET().GetModel(eval.CE_USER);
                string sql = new BLL.UserCommonBL().UserCommentEdit(ceid, jf, status);

                SqlHelper.ExecuteNonQuery(SqlHelper.LocalSqlServer, new string[] { sql});

                if (eval.BYZD1 != 1)
                {
                    sql = sql + ";insert into IntegralWasteBook(IntergalId,IntergalType,IntergalName,IntergalRange,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7)values('" + Guid.NewGuid().ToString() + "','" + Convert.ToString((int)BLL.InergalManage.InergalTypeEnum.评论积分) + "','" + ceid.ToString() + "'," + jf + ",'" + DateTime.Now.ToString() + "','" + eval.CE_USER + "','" + BLL.InergalManage.InergalTypeEnum.评论积分.ToString() + "'," + vipset.DQJF + ")";
                }
                int result = 0;
                string key = ConfigurationManager.AppSettings["CODE"];
                LX_Manage.ERP.Service service = new LX_Manage.ERP.Service();
                if (sql != "")
                {
                    if (eval.BYZD1 != 1)
                    {
                        service.UpdateCustomerDQJF(key, eval.CE_USER, jf, "");
                    }
                }
            }
        }
        this.GridView1.EditIndex = -1;
        GetAllComment();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        GetAllComment();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Model.COMMODITY_EVAL eval = (Model.COMMODITY_EVAL)e.Row.DataItem;

            TextBox txtJF = (TextBox)e.Row.Cells[6].FindControl("txtJF");
            txtJF.Text = SiteCommon.Common.GetDecimal(eval.AddJF, 0).ToString();
            txtJF.Enabled = false;

            DropDownList drpStatus = (DropDownList)e.Row.Cells[8].FindControl("DdrpStatus");
            drpStatus.DataSource = statusList;
            drpStatus.DataBind();
            drpStatus.SelectedIndex = SiteCommon.Common.GetInt(eval.Show, 0);
            drpStatus.Enabled = false;


        }

    }

    #region   评价内容长度
    /// <summary>
    /// 评价内容长度
    /// </summary>
    /// <param name="strContentBack"></param>
    /// <returns></returns>
    public string getUserComment(string strUserComment)
    {
        return strUserComment.Length > 10 ? strUserComment.Substring(0, 8) + ".." : strUserComment.Length < 1 ? "---" : strUserComment;
    }

    #endregion
    #region  获取状态


    public string getshowFlag(int i)
    {
        return i == 1 ? "显示" : "隐藏";
    }

    #endregion
    #region  操作状态


    public string getOperationshowFlag(int i)
    {
        return i == 1 ? "隐藏" : "显示";
    }

    #endregion

    #region 获取所有评价

    private void GetAllComment()
    {
        BLL.UserCommonBL myBL = new UserCommonBL();
        int recordCount = 0;
        List<Model.COMMODITY_EVAL> myCommentList = new List<Model.COMMODITY_EVAL>();
        string ret = myBL.GetAllComment(this.CutPageBar1.PageIndex, this.CutPageBar1.PageSize, out recordCount, out myCommentList);
        if (ret.Equals("000000"))
        {
            this.CutPageBar1.RecordCount = recordCount;
            this.GridView1.DataSource = myCommentList;
            this.GridView1.DataBind();
        }
        
    }

    #endregion
    #region   分页控件事件
    /// <summary>
    ///  分页控件事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CutPageBar1_OnPageIndexChanged(object sender, SiteCommon.ChangePageArgs e)
    {
        this.CutPageBar1.PageIndex = e.PageIndex;
        this.GetAllComment();

    }
    #endregion

}
